<?php

class TasksController extends Controller
{
	public function actionIndex()
	{
                $pid = Yii::app()->request->getParam("pid");
                $uid = Yii::app()->request->getParam("uid");
                // get all tasklist assocative with project
                $modelTasklists = TaskList::model()->findAllByAttributes(array('tasklist_project_id'=>$pid),array('order'=>"task_list_id DESC"));
                $tasklists = array();
                foreach ($modelTasklists as $modelTasklist){
                    $listTasks = $modelTasklist->tasks;
                    $tasklist['listTasks'] = $listTasks;
                    $tasklist['model']     = $modelTasklist;
                    $tasklists[] = $tasklist;
                }
                
                // get all user assocative with project
                $modelProject = Project::model()->findByPk($pid);
                Yii::log($modelProject->project_id);
                $users = $modelProject->projectUsers;
                $tasklistModel = new TaskList();
                $taskModel = new Task();
                $this->render('index',array(
                    'tasklists'=>$tasklists,
                	'taskModel'=>$taskModel,
                    'users'=>$users,
                	'tasklistModel'=>$tasklistModel,
                ));
	}
	public function actionDetail()
	{
                // GET app parameter
                $tid = Yii::app()->request->getParam("tid");
                $task = Task::model()->findByPk($tid);
                
                // GET all comment associative with tasks
                $comments = Comment::model()->findAllByAttributes(array("comment_object_type"=>"TASK","comment_object_id"=>$tid));
                $this->render('detail',array(
                    'task'=>$task,
                    'comments'=>$comments,
                ));
	}
	
	public function actionDel()
	{
                // GET app parameter
                $tid = Yii::app()->request->getParam("tid");
                if ($tid == null)
                	throw new CHttpException(404,'Param not enough');
                	
                $task = Task::model()->findByPk($tid);
                if (!$task || !$task->delete()){
                	throw new CHttpException(404,"Can't delete");
                }
	}
        
        /**
         * action for add new comment
         * return html code in view for new comment
         */
        public function actionCreateComment()
        {
                $this->layout = "/ajax/main.php";
                $uid = Yii::app()->user->id;
                $commentBody = Yii::app()->request->getPost("commentBody");
                $pid = Yii::app()->request->getPost("pid");
                $tid = Yii::app()->request->getPost("tid");
                
                $comment = new Comment();
                $comment->comment_content = $commentBody;
                $comment->comment_posted_date = new CDbExpression('NOW()');
                $comment->comment_posted_by_user_id = $uid;
                $comment->comment_project_id =$pid;
                $comment->comment_object_id = $tid;
                $comment->comment_object_type = "TASK";
                $comment->save();
                
                $this->render('comment',array(
                    'comment'=>$comment,
                ));
        }
        
        /**
         * add new task list
         */
        public function actionAddTaskList() {
                // get all parameter
                $pid = Yii::app()->request->getParam("pid");
                $tasklist = new TaskList();
                if ($pid == null)
                	throw new CHttpException(404,'Param not enough');
                
                $this->performAjaxValidation($tasklist);
                if (isset($_POST['TaskList'])) {
                	$tasklist->attributes = $_POST['TaskList'];
                	$tasklist->tasklist_project_id = 1;
                	if ($tasklist->save()) {
                		Yii::app()->user->setFlash('success','New task list created successful');
                		$this->redirect(array('/tasks/index','pid'=>$pid));
                		return;
                	}
                	Yii::app()->user->setFlash('error','Error create new task list');
                	$this->redirect(array('/tasks/index','pid'=>$pid));
                }
        }
        
        public function performAjaxValidation($model) {
        	if (isset($_POST['ajax'])) {
        		echo CActiveForm::validate($model);
        		Yii::app()->end();
        	}
        }
        
        /**
         * edit task list
         */
        public function actionEditTaskList(){
        		// get param
        		$tl_name = Yii::app()->request->getPost('taskListName');
        		$tl_description = Yii::app()->request->getPost('taskListDes');
        		$tl_id = Yii::app()->request->getPost('tl_id');
        		if (($tl_name == NULL) or ($tl_description == NULL) or( $tl_id == NULL))
        			throw new CHttpException(404, 'Param is not enough');
        			
        		$tl = TaskList::model()->findByPk($tl_id);
        		$tl->task_list_name = $tl_name;
        		$tl->task_list_description = $tl_description;
        		if (!$tl->save()) {
        			throw new CHttpException(404, "Can't save");
        		}
        }
        
        /**
         * delete task list
         */
        public function actionDelTaskList() {
        		//get param
        		$tl_id = Yii::app()->request->getPost('tl_id');
        		if ($tl_id == null)
        			throw new CHttpException(404,'Param not enough');
        			
        		$tl = TaskList::model()->findByPk($tl_id);
        		if ( !$tl || !$tl->delete())
        			throw new CHttpException(404,'Error delete task list');
        }
        
        /**
         * Add task
         */
        public function actionAdd() {
        	//get param
        	$pid = Yii::app()->request->getParam("pid");
        	if ($pid == null)
				throw new CHttpException(404,'Param not enough');
				
        	$model = new Task();
        	$this->performAjaxValidation($model);
			if (isset($_POST['Task'])) {
				$model->attributes = $_POST['Task'];
				$model->task_created_by_user_id = Yii::app()->user->id;
				$model->task_created_date = new CDbExpression('NOW()');
				
				if ($model->save()) {
                		Yii::app()->user->setFlash('success','New task created successful');
                		
                		// save notify people
                		if (isset($_POST['list-user'])){
                			foreach ( $_POST['list-user'] as $userId) {
                				$task_user = new TaskUser();
                				$task_user->task_user_task_id = $model->task_id;
                				$task_user->task_user_user_id = $userId;
                				$task_user->save();
                			}
                		}
                		
                		$this->redirect(array('/tasks/index','pid'=>$pid));
                		return;
                	}
                Yii::app()->user->setFlash('error','Error create new task');
                //$this->redirect(array('/tasks/index','pid'=>$pid));
            }
        }
        
		/**
         * Add task
         */
        public function actionEdit() {
        	//get param
        	$pid = Yii::app()->request->getParam("pid");
        	$tid = Yii::app()->request->getParam("tid");
        	if  (($pid == null) or ($tid == null))
				throw new CHttpException(404,'Param not enough');
				
        	$model = Task::model()->findByPk($tid);
        	$this->performAjaxValidation($model);
			if (isset($_POST['Task'])) {
				$model->attributes = $_POST['Task'];
				$model->task_created_by_user_id = Yii::app()->user->id;
				$model->task_created_date = new CDbExpression('NOW()');
				
				if ($model->save()) {
                		Yii::app()->user->setFlash('success','New task created successful');
                		
                		// save notify people
                		if (isset($_POST['list-user'])){
                			foreach ( $_POST['list-user'] as $userId) {
                				$task_user = new TaskUser();
                				$task_user->task_user_task_id = $model->task_id;
                				$task_user->task_user_user_id = $userId;
                				$task_user->save();
                			}
                		}
                		
                		$this->redirect(array('/tasks/index','pid'=>$pid));
                		return;
                	}
                Yii::app()->user->setFlash('error','Error create new task');
            }
            $this->render('edit', array('editTask'=>$model));
        }
        
}